/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.cspimp.psp.service.impl;

import me.zhengjie.modules.cspimp.psp.domain.Recheck;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.modules.cspimp.psp.repository.RecheckRepository;
import me.zhengjie.modules.cspimp.psp.service.RecheckService;
import me.zhengjie.modules.cspimp.psp.service.dto.RecheckDto;
import me.zhengjie.modules.cspimp.psp.service.dto.RecheckQueryCriteria;
import me.zhengjie.modules.cspimp.psp.service.mapstruct.RecheckMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;

/**
* @website https://docs.auauz.net
* @description 服务实现
* @author zmm
* @date 2020-06-26
**/
@Service
@RequiredArgsConstructor
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class RecheckServiceImpl implements RecheckService {

    private final RecheckRepository recheckRepository;
    private final RecheckMapper recheckMapper;

    @Override
    public Map<String,Object> queryAll(RecheckQueryCriteria criteria, Pageable pageable){
        Page<Recheck> page = recheckRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(recheckMapper::toDto));
    }

    @Override
    public List<RecheckDto> queryAll(RecheckQueryCriteria criteria){
        return recheckMapper.toDto(recheckRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    public RecheckDto findById(Long id) {
        Recheck recheck = recheckRepository.findById(id).orElseGet(Recheck::new);
        ValidationUtil.isNull(recheck.getId(),"Recheck","id",id);
        return recheckMapper.toDto(recheck);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RecheckDto create(Recheck resources) {
        return recheckMapper.toDto(recheckRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Recheck resources) {
        Recheck recheck = recheckRepository.findById(resources.getId()).orElseGet(Recheck::new);
        ValidationUtil.isNull( recheck.getId(),"Recheck","id",resources.getId());
        recheck.copy(resources);
        recheckRepository.save(recheck);
    }

    @Override
    public void deleteAll(Long[] ids) {
        for (Long id : ids) {
            recheckRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<RecheckDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (RecheckDto recheck : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("创建时间", recheck.getCreateTime());
            map.put("更新时间", recheck.getUpdateTime());
            map.put("创建者", recheck.getCreateBy());
            map.put("更新者", recheck.getUpdateBy());
            map.put(" cid",  recheck.getCid());
            map.put("所属项目", recheck.getPid());
            map.put("复检对象名称", recheck.getObjName());
            map.put("复检对象类型", recheck.getObjType());
            map.put("复检周期", recheck.getCycle());
            map.put("应复检日期", recheck.getRecheckDate());
            map.put("实际复检日期", recheck.getActualRecheckDate());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}